package com.hq.es.controller;

import com.hq.es.dto.UpdateDocumentDTO;
import com.hq.es.utils.ElasticsearchUtil;
import com.hq.es.dto.CreateDocumentDTO;
import com.hq.es.vo.EsPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.runtime.regexp.JoniRegExp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

@Api(tags = "文档数据操作")
@RequestMapping("/document")
@RestController
@Slf4j
public class DocumentController {

    @Resource
    private ElasticsearchUtil elasticsearchUtil;

    @PostMapping
    @ApiOperation(value = "新增文档记录")
    public Object createDocument(@RequestBody CreateDocumentDTO indexCreateDTO) throws IOException {
        String index = indexCreateDTO.getIndex();
        Map<String, Object> document = indexCreateDTO.getDocument();
        return elasticsearchUtil.addData(document, index);
    }


    @DeleteMapping("/{index}/{documentId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index",paramType = "path",value = "索引名称",required = true),
            @ApiImplicitParam(name = "documentId",paramType = "path",value = "文档id",required = true)
    })
    @ApiOperation(value = "删除文档记录")
    public Object deleteDocument(@PathVariable String index,@PathVariable String documentId) throws IOException {
        return elasticsearchUtil.deleteDataById(index,documentId ).getResult();
    }

    @PutMapping
    @ApiOperation(value = "修改文档记录")
    public Object updateDocument( @RequestBody UpdateDocumentDTO updateDocumentDTO) throws IOException {
        String documentId = updateDocumentDTO.getDocumentId();
        String index = updateDocumentDTO.getIndex();
        Map<String, Object> document = updateDocumentDTO.getDocument();
        return elasticsearchUtil.updateDataById(document,index,documentId );
    }

    @GetMapping("/{index}/{documentId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index",paramType = "path",value = "索引名称",required = true),
            @ApiImplicitParam(name = "documentId",paramType = "path",value = "文档id",required = true)
    })
    @ApiOperation(value = "根据id查询文档")
    public Object queryDocumentById( @PathVariable String index,@PathVariable String documentId) throws IOException {
        return elasticsearchUtil.searchDataById(index,documentId,"" );
    }




    @GetMapping("/exist/{index}/{documentId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index",paramType = "path",value = "索引名称",required = true),
            @ApiImplicitParam(name = "documentId",paramType = "path",value = "文档id",required = true)
    })
    @ApiOperation(value = "根据ID判断文档是否存在")
    public Object queryDocumentExist( @PathVariable String index,@PathVariable String documentId) throws IOException {
        return elasticsearchUtil.existsById(index,documentId);
    }

}
